cc.Class({
    extends: cc.Component,
    ctor(){
        this._initTime = Date.now();
    },
    properties: {

    },

    start () {

    },
    /*
        获取当天日期
        return: String 'yyyymmdd'
    */
    getCurDate(){
        let date = new Date();
        let month = date.getMonth()+1;
        month = String(month<10?'0'+month:month);
        let day = date.getDate();
        day = String(day<10?'0'+day:day);
        return ''+date.getFullYear()+month+day
    },
    /*
        获取贝塞尔曲线经过得点
        params: 
            s: cc.v2 起始点
            c: cc.v2 控制点
            e: cc.v2 终点
        return:
            Array:[cc.v2]
    */
    getBezlPoint(s, c, e){
        //控制点
        let aP = [];
        for(let i=0; i<=100; i+=20){
            let t = i/100;
            let msLen = t*c.sub(s).len();
            let ncLen = t*e.sub(c).len();
            let pm = s.add(c.sub(s).normalize().mul(msLen));
            let pn = c.add(e.sub(c).normalize().mul(ncLen));
            let mnLen = t*pn.sub(pm).len();
            let pa = pm.add(pn.sub(pm).normalize().mul(mnLen));
            aP.push(pa)
        }
        aP.shift();
        return aP;
    },
    /*
        获取Url参数
        params
            key: String 参数名
        return 
            String
    */
    getQS(key){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == key) {
                return pair[1];
            }
        }
        return '';
    },
    /*
        画虚线
        params:
            graphic: [cc.Graphic]
            lineDis: [Number]
            pointB: [cc.v2]
    */    
    drawLine(graphic, lineDis, pointB){
        let len = pointB.len();
        let n = Math.ceil(len / lineDis);  
        let normal = pointB.normalize();
        let o = cc.v2(0,0)
        graphic.moveTo(o.x, o.y);
        for(let i=0; i<n-1; i++){
            o = o.add(normal.mul(lineDis/2));
            graphic.lineTo(o.x, o.y)
            o = o.add(normal.mul(lineDis/2));
            graphic.moveTo(o.x, o.y)
        }
        graphic.lineTo(pointB.x, pointB.y);
    },
    /*
        获取游戏配置
        return: Promise
    */
    getConf(){
        return new Promise((resolve, reject)=>{
            axios({
                method: "POST",
                url: 'https://mp.xunbao88.com.cn/wechatbackend/activityAction.do?method=getGameInfoById',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                transformRequest: [
                    function (data) {
                        let ret = ''
                        for (let it in data) {
                            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                        }
                        ret = ret.substring(0, ret.lastIndexOf('&'));
                        return ret
                    }
                ],
                data: {
                    token: this.getQS('token'),
                    fromType: this.getQS('fromType'),
                    groupId: this.getQS('groupId'),
                }
            }).then(res=>{
                resolve(res.data)
            }).catch(err=>{
                reject(err)
            })
        })    
    },

    /*
        提交分数
        params: 
            score: Number|String 游戏得分
            themeType: Number|String 当前游戏主题
        return: Promise
    */
    submitScore(score, cb){
        return new Promise((resolve, reject)=>{
            axios({
                method: 'post',
                url: 'https://mp.xunbao88.com.cn/wechatbackend/activityAction.do?method=postGameNew',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                transformRequest: [
                    function (data) {
                        let ret = ''
                        for (let it in data) {
                            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                        }
                        ret = ret.substring(0, ret.lastIndexOf('&'));
                        return ret
                    }
                ],
                data: {
                    token: this.getQS('token'),
                    fromType: this.getQS('fromType'),
                    groupId: this.getQS('groupId'),
                    themeType: 12345,
                    status: 1,
                    number: score,
                    timeuse: Date.now() - this._initTime,
                    level: 1,
                    gameData: ''
                }
            }).then(res=>{
                this._initTime =  Date.now();
                cb && cb();
                resolve(res);
            }).catch(err=>{
                reject(err)
            })        
        })
    },
    /*
        提交分数V2
        params: 
            score: Number|String 游戏得分
            xileBean: Number 获得喜乐豆数量
            time: Number|String 时间戳
            callBack: 回调函数
        return: Promise    
    */
    submitScoreV2(score, xileBean, callBack){
        axios({
            method: 'post',
            url: 'https://mp.xunbao88.com.cn/wechatbackend/activityAction.do?method=postGameNewV2',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            transformRequest: [
                function (data) {
                    let ret = ''
                    for (let it in data) {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                    ret = ret.substring(0, ret.lastIndexOf('&'));
                    return ret
                }
            ],
            data: {
                token: this.getQS('token'),
                fromType: this.getQS('fromType'),
                groupId: this.getQS('groupId'),
                //分数
                number: score,
                //喜乐豆
                xileBean: xileBean,
                timeuse: Date.now() - this._initTime,
            }
        }).then(res=>{
            this._initTime =  Date.now();
            callBack && callBack();
        }) 
    },
    /*
        缓存游戏数据
    */
    cacheData(gameData){
        axios({
            method: "POST",
            url: 'https://mp.xunbao88.com.cn/wechatbackend/activityAction.do?method=addUserUndoneGameInfo',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            transformRequest: [
                function (data) {
                    let ret = ''
                    for (let it in data) {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                    ret = ret.substring(0, ret.lastIndexOf('&'));
                    return ret
                }
            ],
            data: {
                token: this.getQS('token'),
                fromType: this.getQS('fromType'),
                groupId: this.getQS('groupId'),
                gameData: gameData,         
            }
        }).then(res=>{
    
        }).catch(err=>{
            
        })
    },
    /*
        获取缓存数据
    */
    getCacheData(){
        return new Promise((resolve, reject)=>{
            axios({
                method: "POST",
                url: 'https://mp.xunbao88.com.cn/wechatbackend/activityAction.do?method=getUserUndoneGameInfo',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                transformRequest: [
                    function (data) {
                        let ret = ''
                        for (let it in data) {
                            ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                        }
                        ret = ret.substring(0, ret.lastIndexOf('&'));
                        return ret
                    }
                ],
                data: {
                    token: this.getQS('token'),
                    fromType: this.getQS('fromType'),
                    groupId: this.getQS('groupId'),
                }
            }).then(res=>{
                resolve(res.data)
            }).catch(err=>{
                reject(err)
            })
        })
    },
    update (dt) {


    },
});
